﻿using System.Collections.Generic;
using Cms.Application.Admin.Views;
using Cms.Application.IO;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class MasterTemplatesPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IMasterTemplatesView _view;
        public IDiskAccess DiskAccess;

        public MasterTemplatesPresenter(IMasterTemplatesView view)
            : this(view, new RepositoryFactory())
        {
            DiskAccess = new DiskAccess();
        }

        public MasterTemplatesPresenter(IMasterTemplatesView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.MasterTemplates)
                _view.RedirectToPermissionDeniedPage();
        }

        public void Initialize()
        {
            SetMasterTemplates();
        }
        
        public void SetMasterTemplates()
        {
            _view.SetMasterTemplates(_administrator.CurrentSite.MasterTemplates);
        }

        public void DeleteMasterTemplate(int id)
        {
            MasterTemplate mt = SiteRepository.GetMasterTemplate(id);
            if (mt.Pages.Count == 0)
            {
                PerformDelete(mt);
            }
            else if (_administrator.CurrentSite.MasterTemplates.Count > 1)
            {
                //Let user select an alternative mastertemplate for the associated pages
                IList<MasterTemplate> replacements = new List<MasterTemplate>();
                foreach (MasterTemplate replacement in _administrator.CurrentSite.MasterTemplates)
                    if (replacement.ID != id)
                        replacements.Add(replacement);
                _view.SetReplacements(replacements, mt.Name);
            }
            else
            {
                _view.DisplayUnableToDeleteAlert(mt.Name);
            }
        }

        public void ReplaceAndDeleteMasterTemplate(int id, int replacementID)
        {
            MasterTemplate mt = SiteRepository.GetMasterTemplate(id);
            MasterTemplate replacement;
            while (mt.Pages.Count > 0)
            {
                Page p = mt.Pages[0];
                replacement = SiteRepository.GetMasterTemplate(replacementID);
                p.MasterTemplate = replacement;
                replacement.Pages.Add(p);
                mt.Pages.Remove(p);
            }
            SiteRepository.SaveSite(_administrator.CurrentSite);
            PerformDelete(mt);
        }
        
        private void PerformDelete(MasterTemplate mt)
        {
            mt.Site.MasterTemplates.Remove(mt);
            if (mt.Site.DefaultMasterTemplate.ID == mt.ID)
                mt.Site.DefaultMasterTemplate = mt.Site.MasterTemplates.Count > 0 ? mt.Site.MasterTemplates[0] : null;
            mt.Site = null;
            foreach (MasterTemplateStylesheet mts in mt.AssociatedStylesheets)
            {
                mts.Stylesheet.AssociatedMasterTemplates.Remove(mts);
                mts.MasterTemplate = null;
                mts.Stylesheet = null;
            }
            mt.AssociatedStylesheets.Clear();
            foreach (MasterTemplateContentLink mtl in mt.Content)
            {
                mtl.MasterTemplate = null;
            }
            mt.Content.Clear();
            SiteRepository.DeleteMasterTemplate(mt.ID);
            SiteRepository.SaveSite(_administrator.CurrentSite);
            DiskAccess.DeleteFile("__System\\MasterTemplates\\" + mt.ID + ".master");
            SetMasterTemplates();
        }
    }
}